import { Controller, Get, Post, Body, Patch, Param, Delete, Res, Put } from '@nestjs/common';
import { FoodsService } from './foods.service';
import { Creat_Food } from "../Types/Food"

@Controller('foods')
export class FoodsController {
  constructor(private readonly foodsService: FoodsService) { }

  @Post()
  async create(@Body() body: Creat_Food, @Res() res) {
    const result = await this.foodsService.create(body)
    if (result) {
      res.status(200).send({
        status: 200,
        message: "添加成功"
      })
    } else {
      res.status(426).send({
        status: 426,
        message: "添加失败"
      })
    }
  }

  @Put('/addview')
  async addviews(@Body() Body, @Res() res) {
    console.log(Body)
    const result: any = await this.foodsService.addviews(Body.id)
    if (!result) {
      res.status(201).send({
        status: 201,
        message: ""
      })
    } else {
      if (result.matchedCount == 0) {
        res.status(425).send({
          status: 425,
          message: "修改失败"
        })
      } else {
        res.status(200).send({
          status: 200,
          message: "成功"
        })
      }

    }
  }
  @Get('/userfood/:id/:skip')
  async userfood(@Param() param, @Res() res) {
    const result = await this.foodsService.userfood(param.id, param.skip)
    console.log(param)
    res.status(200).send({
      status: 200,
      message: "ok",
      data: result
    })
  }
  @Get('/category/:skip/:type')
  async category(@Param() Param, @Res() res) {
    const result = await this.foodsService.category(Param.skip, Param.type)

    res.send({
      status: 200,
      message: "ok",
      data: result
    })
  }
  @Get('/:id')
  async findOne(@Res() res, @Param() params) {
    const result = await this.foodsService.findOne(params.id)
    if (result) {
      res.status(200).send({
        message: "ok",
        data: result
      })
    } else {
      res.status(500).send({
        message: "获取失败",
        data: {}
      })
    }
  }
  @Post('/searchlist')
  async searchlist(@Res() res, @Body() body) {
    const { keyword } = body
    console.log(body)
    const result = await this.foodsService.searchlist(keyword)
    console.log(result, '?')
    if (result) {
      res.status(200).send({
        message: "ok",
        data: result
      })
    } else {
      res.status(500).send({
        message: "获取失败",
        data: {}
      })
    }
  }
  @Post('/findall/:skip/:type/:order')
  async findAll(@Param() param, @Body() body) {
    const { keyword } = body
    console.log(body)
    const result = await this.foodsService.findAll(param.skip, param.type, keyword, param.order)
    // console.log(result)
    return {
      message: "ok",
      data: result.data,
      count: result.count
    }
  }

  @Delete('/deleteone')
  async deleteone(@Body() body, @Res() res) {
    const { id } = body
    const result = await this.foodsService.deleteOne(id)
    if (!result) {
      res.status(427).send({
        status: 427,
        message: '删除失败'
      })

    } else {
      if (result.deletedCount == 1) {
        res.status(200).send({
          status: 200,
          message: '成功'
        })
      } else {
        res.status(427).send({
          status: 427,
          message: '删除失败'
        })
      }

    }


  }
  @Get('/food/list/:id')
  async foodlist(@Res() res, @Param() params) {
    const result = await this.foodsService.foodlist(params.id)

    res.status(200).send({
      status: 200,
      message: "获取成功",
      data: result
    })
  }

  @Get('/recomment/list')
  async Recomment(@Res() res) {
    const result = await this.foodsService.Recomment()
    res.status(200).send({
      message: "获取成功",
      data: result
    })
  }
  @Get('/recomment/list/swiper')
  async swiper(@Res() res) {
    const result = await this.foodsService.Recomment2()
    res.status(200).send({
      message: "获取成功",
      data: result
    })
  }
  @Put('/:id')
  async update(@Param() Param, @Body() body: Creat_Food, @Res() res) {
    const result = await this.foodsService.update(Param.id, body)
    console.log(result)
    if (!result) {
      res.status(425).send({
        status: 425,
        message: "修改失败"
      })
    } else {
      if (result.matchedCount == 0) {
        res.status(425).send({
          status: 425,
          message: "修改失败"
        })
      } else {
        res.status(200).send({
          status: 200,
          message: "成功"
        })
      }

    }
  }
}
